<template>
  <div class="mod-config">
    <basic-container>
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item prop="lotNo">
          <el-input placeholder="请输入车位号" clearable v-model="queryParams.lotNo" />
        </el-form-item>
        <el-form-item prop="status">
          <el-select v-model="queryParams.status" clearable class="m-2" placeholder="车位状态">
            <el-option v-for="item in dicts.park_space" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-button v-if="permissions.stop_regionlot_add" icon="el-icon-plus" type="primary"
            @click="addOrUpdateHandle()">新增</el-button>
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column prop="lotNo" header-align="center" align="center" label="车位号">
          </el-table-column>
          <el-table-column prop="status" header-align="center" align="center" label="车位状态">
            <template #="scope">
              <dict-tag :options="dicts.park_space" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column prop="startDate" header-align="center" align="center" label="租赁开始日期">
          </el-table-column>
          <el-table-column prop="endDate" header-align="center" align="center" label="租赁结束日期">
          </el-table-column>
          <el-table-column header-align="center" align="center" label="内部客户">
          </el-table-column>
          <el-table-column prop="regionName" header-align="center" align="center" label="区域名称">
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作">
            <template #="scope">
              <el-button link type="primary" size="small" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">
                修改
              </el-button>
              <el-button link type="primary" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
          :current-page="queryParams.current" :page-sizes="[10, 20, 50, 100]" :page-size="queryParams.size"
          :total="totalPage" background layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" :regionNames="dataForm"
        :regionId="queryParams">
      </table-form>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj } from '@/api/stop/regionlot'
import TableForm from './regionlot-form.vue'
import { mapGetters } from 'vuex'
export default {
  dicts: ['park_space'],
  data () {
    return {
      queryParams: {
        current: 1,
        size: 10,
        regionId: this.$route.query.regionId
      },

      dataForm: {
        regionNames: null
      },
      dataList: [],
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    TableForm
  },
  created () {
    this.getDataList()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      fetchList(Object.assign(this.queryParams)).then(response => {
        this.dataList = response.data.data.records
        if (response.data.data.records.length > 0) {
          this.dataForm.regionNames = response.data.data.records[0].regionName
        }
        this.totalPage = response.data.data.total
      })
      this.dataListLoading = false
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm('是否确认删除ID为' + id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      }).catch(() => { })
    },
    handleQuery () {
      this.queryParams.current = 1;
      this.getDataList()
    },
    resetQuery () {
      this.queryParams = {
        current: 1,
        size: 10,
        regionId: this.$route.query.regionId
      };
      //   this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>
